package com.lwq.project.controller.mune;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lwq.project.model.Mune;
import com.lwq.project.model.custom.CustomMune;
import com.lwq.project.service.IconService;
import com.lwq.project.service.MuneService;
import com.lwq.project.utils.UtilsTools;
import com.lwq.project.utils.page.Pages;

@Controller
@RequestMapping("/mune")
public class MuneController {

	@Autowired
	private MuneService muneService;
	
	@Autowired
	private IconService iconService;
	
	@RequestMapping("/muneView.action")
	public String initView(){
		return "main/mune/muneView";
	}
	
	@RequestMapping("/muneTable.action")
	public String initList(Model model,Pages pages){
		Map<String ,Object> map = new HashMap<String,Object> ();
		map.put("pages", pages);
		List<CustomMune> muneList = muneService.getPageMune(map);
		int count = muneService.countMune();
		pages.setNumberCount(count);
		model.addAttribute("pages",pages);
		model.addAttribute("muneList", muneList);
		return "main/mune/muneTable";
	}
	
	@RequestMapping("/muneEdit.action")
	public String initEdit(Model model,String mId){
		if(!StringUtils.isBlank(mId)){
			model.addAttribute("mune", muneService.getMuneById(mId));
		}
		model.addAttribute("parentMune", muneService.getMuneByGrade("1"));
		model.addAttribute("iconList", iconService.getAllIcon());
		return "main/mune/muneEdit";
	}
	
	
	@RequestMapping("/muneSave.action")
	@ResponseBody
	public String initSave(Mune mune){
		if(!StringUtils.isBlank(mune.getmId())){
			muneService.updateMune(mune);
		}else{
			mune.setmId(UtilsTools.getPrimary());
			muneService.insertMune(mune);
		}
		return "success";
	}
	@RequestMapping("/muneDel.action")
	@ResponseBody
	public String initDelete(@RequestParam(required = false,value = "mIds[]")  String[] mIds){
		muneService.deleteMune(mIds);
		return "success";
	}
}
